﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRMSalesManagement.Manage.FrontPageManagement
{
    public partial class ManageNewsList : System.Web.UI.Page
    {
        private static string newsType = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.lblResult.Text = "";
                GridViewBinding();
            }

            if (Request.QueryString["type"] != null)
            {
                HyperLink1.NavigateUrl += Request.QueryString["type"].ToString();
                //CommonSiteMap.setType(Request.QueryString["type"].ToString());
                //CommonSiteMap.foreOrBack = false;
            }
        }
        /// <summary>
        /// 绑定GridView和分页控件
        /// </summary>
        private void GridViewBinding()
        {
            int pageNum = 1;
            if (Request.QueryString["type"] != null)
            {

                newsType = Request.QueryString["type"].ToString();
            }
            if (Request.QueryString["page"] != null)
            {

                pageNum = int.Parse(Request.QueryString["page"]);
            }
            int total = 0;
            var columnDb =CRM.Business.NewList.ManageListNews(out total, pageNum, this.DataPage1.ShowRecordCount, newsType);
            //this.DataPage1.TotalRecordCount = total;
            //this.DataPage1.ShowPageNumberCount = pageNum;

            if (columnDb != null && columnDb.Count > 0)
            {
                this.gvNewsList.DataSource = columnDb;
                this.gvNewsList.DataBind();
            }
            else if (pageNum > 1)
            {
                pageNum = pageNum - 1;
                columnDb = CRM.Business.NewList.ManageListNews(out total, pageNum, this.DataPage1.ShowRecordCount, newsType);
                this.DataPage1.TotalRecordCount = total;
                this.DataPage1.ShowPageNumberCount = pageNum;
                if (columnDb != null)
                {
                    this.gvNewsList.DataSource = columnDb;
                    this.gvNewsList.DataBind();
                }
            }
            #region 控制分页控件
            CRM.Business.ShowPageDispaly.NewPageDispaly(this.DataPage1, pageNum, total);
            #endregion

        }

        protected void gvNewsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(gvNewsList.DataKeys[e.RowIndex].Value);
            var news =CRM.Business.NewList.GetNewById(id);
            if (news != null)
            {
                bool result =CRM.Business.NewList.DeleteNews(news);
                if (result)
                {
                    this.lblResult.Text = "*删除成功";
                }
                else
                {
                    this.lblResult.Text = "*删除失败";
                }
                Response.Redirect("/Manage/FrontPageManagement/ManageNewsList.aspx");
                //GridViewBinding();
            }
        }

        protected void gvNewsList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            int id = Convert.ToInt32(gvNewsList.DataKeys[e.NewEditIndex].Value);
            Session["newsId"] = id;
            Session["type"] = newsType;
            newsType = Session["type"].ToString();
            string url = "NewsEdit.aspx?";
            url += id;
            url += newsType;
            Response.Redirect(url);
        }
    }
}